package com.tnl.web.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.biz.hunter.dbutil.BaseDAO;
import com.tnl.web.domain.Permission;


@Repository
public class PermissionDAO {
	private static final String SQL_GET_ID = "select * from tb_permission where id = ?";
	private static final String SQL_GET_NAME = "select * from tb_permission  where Name = ?";
	private static final String SQL_GET_ALL = "select * from tb_permission ";
	private static final String ROLE_PERMISSION= "select * from tb_permission where idpermission in (select permissionId from tb_role_permission where roleId = ?)";
	private static final String QUERY_PERMISSION ="select * from tb_permission p where idpermission in (select permissionId from tb_role_permission where roleId = ?) and permissionname =?";
	
	public List<Permission> getALL() {
		return BaseDAO.queryForList(Permission.class, SQL_GET_ALL);
	}
	
	public Permission getById(int Id) {
		return  BaseDAO.queryForObject(Permission.class, SQL_GET_ID, Id);
	}
	public Permission getByName(String name){
		return  BaseDAO.queryForObject(Permission.class, SQL_GET_NAME, name);
	}

	public List<Permission> getRolesPermission(int roleId) {
		return BaseDAO.queryForList(Permission.class, ROLE_PERMISSION,roleId);
	}
	
	public boolean checkPermission(int roleId,String name) {
		List<Permission> list = BaseDAO.queryForList(Permission.class, QUERY_PERMISSION,roleId,name);
		if(list == null)
		{
			return false;
		}
		if (list.size() < 1)
		{
			return false;
		}
		return true;
			
	}

	
	
}
